package com.qvtu.springboot.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qvtu.springboot.entity.Employee;
import com.qvtu.springboot.mapper.EmployeeMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {
    @Resource
    private EmployeeMapper employeeMapper;

    public List<Employee> selectAll() {
        List<Employee> list = employeeMapper.selectAll();
        return list;
    }

    public Employee selectById(Integer id) {
        Employee employee = employeeMapper.selectById(id);
        return employee;
    }

    public PageInfo<Employee> selectPage(Integer pageNum, Integer pageSize) {
//      PageHelper.startPage() 是 MyBatis 分页插件的核心方法，用于启动分页功能。
        PageHelper.startPage(pageNum, pageSize);
        List<Employee> list = employeeMapper.selectAll();
//        PageInfo.of()它会根据传入的 list（查询结果）封装分页信息，包括当前页的数据、总记录数、总页数等。
        return PageInfo.of(list);
    }

    public void add(Employee employee) {
        employeeMapper.add(employee);
    }

    public void deleteById(Integer id) {
        employeeMapper.deleteById(id);
    }

    public void update(Employee employee) {
        employeeMapper.update(employee);
    }
}
